package com.smy.common;
import java.lang.reflect.Field;
public class SqlHelper{
    private SqlHelper(){}
    //根据驼峰命名返回下划线命名;
    public static String getName(String pojoName){
        String tableName = pojoName;
        if(tableName.charAt(0)<91){
            tableName = (char)(tableName.charAt(0) + 32) + tableName.substring(1) ;
        }
        for (int i = 1; i < tableName.length(); i++) {
            char c = tableName.charAt(i);
            if (c < 91){
                c = (char) (c + 32);
                String second = "_" + c + tableName.substring(i + 1);
                tableName = tableName.substring(0,i) + second;
            }
        }
        return tableName;
    }
    //根据字段数组返回中间有,的字符串
    public static String getFields(Field[] fields){
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < fields.length; i++) {
            //先变成下划线命名
            stringBuilder.append(SqlHelper.getName(fields[i].getName()));
            //判断是否要拼,
            if (i != fields.length-1) stringBuilder.append(",");
        }
        return stringBuilder.toString();
    }
    //根据字段数组返回set后面的东西,即 : 字段名称 = ? ,字段名称 = ? .....
    public static String getFieldsByUpdate(Field[] fields){
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < fields.length; i++) {
            //下划线命名 = ?
            stringBuilder.append(SqlHelper.getName(fields[i].getName())).append(" = ?");
            //判断是否要拼,
            if (i != fields.length-1) stringBuilder.append(",");
        }
        return stringBuilder.toString();
    }
}
